<?php
header("content-type:text/html;charset=utf-8");
class SysfieldAction extends Action{
     private $field_type;
	 private $field_value_fill_type;
	 private $field_output_type;
	 private $thumb_type;
	 private $wm_type;
	 private $wm_pos;
     public function _initialize(){
	    require_once("system/Config.class.php");
		$this->field_type=$field_type;
		$this->field_value_fill_type=$field_value_fill_type;
		$this->field_output_type=$field_output_type;
		$this->thumb_type=$thumb_type;
		$this->wm_type=$wm_type;
		$this->wm_pos=$wm_pos;
		if(empty($_SESSION["admin"]))$this->redirect("Login/index");  
	 }
     public function index(){
	     $table=$_GET["table"];
		 $sysfield=M("sysfield");
		 //$sysfield->getDbFields();
		 $field_arr=$sysfield->where("col_eng_name='$table'")->order("sequence desc")->findAll();
		 $maxseq=$sysfield->where("col_eng_name='$table'")->max("sequence");
		 $minseq=$sysfield->where("col_eng_name='$table'")->min("sequence");
		 $this->assign("maxseq",$maxseq);
		 $this->assign("minseq",$minseq);
		 $this->assign("field_arr",$field_arr);
		 $this->assign("table",$table);
		 $this->assign("field_type",$this->field_type);
		 $this->assign("field_value_fill_type",$this->field_value_fill_type);
		 $this->assign("field_output_type",$this->field_output_type);
		 $this->assign("thumb_type",$this->thumb_type);
		 $this->assign("wm_type",$this->wm_type);
		 $this->assign("wm_pos",$this->wm_pos);
		 
		 //求出字段分组数组
		 $sysfield_group=M("sysfield_group");
		 $sg=$sysfield_group->where("col_eng_name='$table'")->order("sequence desc")->findAll();
		 $sg_list[0]="请选择分组";
		 foreach($sg as $k=>$v){
			 $sg_list[$v['id']]=$v['group_name'];
		 }
		 $this->assign("sg_list",$sg_list);
		 $this->display();
	 }
	 public function insert(){
	    $table=$_GET["table"];
		$field_name=$_POST["field_name"];
		$field_type=$_POST["field_type"];
		$field_length=$_POST["field_length"];
		$field_comment=$_POST["field_comment"];
		if(!empty($field_length)) 
		   $field_type_length="$field_type($field_length)";
        else
		   $field_type_length="$field_type";
		
		if($field_type=='varchar'||$field_type=='text') 
		   $charset_string="character set utf8 collate utf8_general_ci";
		else
		   $charset_string="";
		$sql="alter table $table add $field_name $field_type_length $charset_string  not null comment '$field_comment'";
	    $sysfield=D("sysfield");
		//执行把新添加的字段添加到$table中
		$sysfield->execute($sql);
		$_POST["col_eng_name"]=$table;
		$seq=$sysfield->where("col_eng_name='$table'")->max("sequence");
		$seq++;
		$_POST["sequence"]=$seq;
		if($vo=$sysfield->create()){
		   if(false!==$sysfield->add()){
		      $this->success("操作成功");
		   }else{
		      $this->error("操作失败");
		   }
		}else{
		   $this->error($sysfield->geterror());
		}
	 }
	 
	 public function delete(){
	    $sysfield=M("sysfield");
		$table=$_GET["table"];
		$field=$_GET["field"];
		$sql="alter table $table drop $field";
		$sysfield->execute($sql);
		$sysfield->startTrans();
		if($sysfield->where("col_eng_name='$table' and field_name='$field'")->delete()){
		    $sysfield->commit();
			$this->success("操作成功");
		}else{
		   $sysfield->rollback();
		   $this->error("操作失败");
		}
	 }
	 
	 public function edit(){
	     $sysfield=M("sysfield");
	     $table=$_GET["table"];
		 $field=$_GET["field"];
		 $field_arr=$sysfield->where("col_eng_name='$table'")->order("sequence desc")->findAll();
		 $maxseq=$sysfield->where("col_eng_name='$table'")->max("sequence");
		 $minseq=$sysfield->where("col_eng_name='$table'")->min("sequence");
		 $this->assign("field_arr",$field_arr);
		 $this->assign("maxseq",$maxseq);
		 $this->assign("minseq",$minseq);
		 $f_arr=$sysfield->where("col_eng_name='$table' and field_name='$field'")->find();
		 $this->assign("f_arr",$f_arr);
		 $this->assign("table",$table);
		 $this->assign("field",$field);
		 $this->assign("field_type",$this->field_type);
		 $this->assign("field_value_fill_type",$this->field_value_fill_type);
		 $this->assign("field_output_type",$this->field_output_type);
		 $this->assign("thumb_type",$this->thumb_type);
		 $this->assign("wm_type",$this->wm_type);
		 $this->assign("wm_pos",$this->wm_pos);
		 
		 //求出字段分组数组
		 $sysfield_group=M("sysfield_group");
		 $sg=$sysfield_group->where("col_eng_name='$table'")->order("sequence desc")->findAll();
		 $sg_list[0]="请选择分组";
		 foreach($sg as $k=>$v){
			 $sg_list[$v['id']]=$v['group_name'];
		 }
		 $this->assign("sg_list",$sg_list);
		 $this->display();	 
	 }
	 
	 public function update(){
	    $table=$_GET["table"];
		$field=$_GET["field"];
		
        if(empty($_POST["field_isdetail"])) $_POST["field_isdetail"]=0;
		if(empty($_POST["field_isuse"])) $_POST["field_isuse"]=0;
		if(empty($_POST["field_islist"])) $_POST["field_islist"]=0;
		if(empty($_POST["is_thumb"])) $_POST["is_thumb"]=0;
		if(empty($_POST["is_wm"])) $_POST["is_wm"]=0;
		$field_name=$_POST["field_name"];
		$field_type=$_POST["field_type"];
		$field_length=$_POST["field_length"];
		$field_comment=$_POST["field_comment"];
		if(!empty($field_length)) 
		   $field_type_length="$field_type($field_length)";
        else
		   $field_type_length="$field_type";
		
		if($field_type=='varchar'||$field_type=='text') 
		   $charset_string="character set utf8 collate utf8_general_ci";
		else
		   $charset_string="";
		$sql="alter table $table change $field  $field_name $field_type_length $charset_string  not null comment '$field_comment'";
	    $sysfield=D("sysfield");
		$sysfield->execute($sql);
		$_POST["col_eng_name"]=$table;
		if($vo=$sysfield->create()){
		   if(false!==$sysfield->save()){
		      $this->redirect("index",array("table"=>$table));
		   }else{
		      $this->error("操作失败");
		   }
		}else{
		   $this->error($sysfield->geterror());
		}
	 }
	
	 public function setup(){
	     $table=$_GET["table"];
		 $field=$_GET["field"];
		 $type=$_GET["type"];
		 $sysfield=M("sysfield");
		 $org_value=$sysfield->where("col_eng_name='$table' and field_name='$field'")->getField($type);
		 $new_value=$org_value?0:1;
		 if($sysfield->where("col_eng_name='$table' and field_name='$field'")->setField($type,$new_value)) 
		 $this->redirect("index",array("table"=>$table));
	 }
	 public function up(){
	    $id=$_GET["id"];
		$table=$_GET["table"];
		$sysfield=M("sysfield");
		$nowseq=$sysfield->where("id=$id")->getField("sequence");
		
		$next=$sysfield->where("col_eng_name='$table' and sequence > $nowseq")->order("sequence asc")->find();
		$nextid=$next["id"];
		$nextseq=$next["sequence"];
		
		$sysfield->where("id=$id")->setField("sequence",$nextseq);
		$sysfield->where("id=$nextid")->setField("sequence",$nowseq);
		$this->redirect("index",array("table"=>$table));
	 }
	 
	 public function down(){
        $id=$_GET["id"];
		$table=$_GET["table"];
		$sysfield=M("sysfield");
		$nowseq=$sysfield->where("id=$id")->getField("sequence");
		
		$next=$sysfield->where("col_eng_name='$table' and sequence < $nowseq")->order("sequence desc")->find();
		$nextid=$next["id"];
		$nextseq=$next["sequence"];
		
		$sysfield->where("id=$id")->setField("sequence",$nextseq);
		$sysfield->where("id=$nextid")->setField("sequence",$nowseq);
		$this->redirect("index",array("table"=>$table));
	 }
	 
	 public function reverse(){
	     $table=$_GET["table"];
		 $sysfield=M("sysfield");
		 $field_arr=$sysfield->where("col_eng_name='$table'")->field("id,sequence")->order("sequence desc")->findAll();
		 $len=count($field_arr);
		 
		 for($i=0;$i<=floor($len/2);$i++){
		     $id=$field_arr[$i]["id"];
			 $seq=$field_arr[$i]["sequence"];
			 
			 $ex_id=$field_arr[$len-$i-1]["id"];
			 $ex_seq=$field_arr[$len-$i-1]["sequence"];
			 
		     $sysfield->where("id=$id")->setField("sequence",$ex_seq);
			 $sysfield->where("id=$ex_id")->setField("sequence",$seq);
		 }
		 $this->redirect("index",array("table"=>$table));
	 }
	 public function reverse_not_redirect($table){//不跳转的反转表顺序
		 $table=$_GET["table"];
		 $sysfield=M("sysfield");
		 $field_arr=$sysfield->where("col_eng_name='$table'")->field("id,sequence")->order("sequence desc")->findAll();
		 $len=count($field_arr);
		 
		 for($i=0;$i<=floor($len/2);$i++){
		     $id=$field_arr[$i]["id"];
			 $seq=$field_arr[$i]["sequence"];
			 
			 $ex_id=$field_arr[$len-$i-1]["id"];
			 $ex_seq=$field_arr[$len-$i-1]["sequence"];
			 
		     $sysfield->where("id=$id")->setField("sequence",$ex_seq);
			 $sysfield->where("id=$ex_id")->setField("sequence",$seq);
		 }
	 }
	public function add_en_field(){
		checkform();
		set_time_limit(0);
		$table=$_GET["table"];
		$en_field=$_POST["en_field"];
		$sysfield=M("sysfield");
        if(strcmp($en_field,"*")>0){
			$en_field_arr=explode(",",$en_field);
			foreach($en_field_arr as $k=>$v){
				$this->reverse_not_redirect($table);
			   	$field_info=$sysfield->where("field_name='$v' and col_eng_name='$table'")->order("sequence desc")->find();
				if(empty($field_info)){
					$this->reverse_not_redirect($table);
					continue;
				}
			    $newfield=array();
				$newfield=$field_info;
				$newfield["field_name"]="en_".$field_info["field_name"];
				$newfield["field_comment"]="英文".$field_info["field_comment"];
				$newfield["field_islist"]=0;
				$newfield["sequence"]=$sysfield->where("col_eng_name='$table'")->max("sequence")+1;
				unset($newfield["id"]);
				$sysfield->add($newfield);
				
				if(!empty($newfield["field_length"])) 
		            $field_type_length=$newfield["field_type"]."(".$newfield["field_length"].")";
                else
		            $field_type_length=$newfield["field_type"];
		
		        if($newfield["field_type"]=='varchar'||$newfield["field_type"]=='text') 
		            $charset_string="character set utf8 collate utf8_general_ci";
		        else
		            $charset_string="";
		        $sql="alter table $table add ".$newfield["field_name"]." ".$field_type_length." ".$charset_string."  not null comment '".$newfield["field_comment"]."'";
		        //执行把新添加的字段添加到$table中
		        $sysfield->execute($sql);
				$this->reverse_not_redirect($table);
			}
		}else{//全部字段
		    $field_list=$sysfield->where("col_eng_name='$table'")->order("sequence desc")->findAll();
			
			$newfield=array();
			foreach($field_list as $k=>$v){
				$this->reverse_not_redirect($table);
				$newfield=$v;
				$newfield["field_name"]="en_".$v["field_name"];
				$newfield["field_comment"]="英文".$v["field_comment"];
				$newfield["field_islist"]=0;
				$newfield["sequence"]=$sysfield->where("col_eng_name='$table'")->max("sequence")+1;
				unset($newfield["id"]);
				$sysfield->add($newfield);
				
				if(!empty($newfield["field_length"])) 
		            $field_type_length=$newfield["field_type"]."(".$newfield["field_length"].")";
                else
		            $field_type_length=$newfield["field_type"];
		
		        if($newfield["field_type"]=='varchar'||$newfield["field_type"]=='text') 
		            $charset_string="character set utf8 collate utf8_general_ci";
		        else
		            $charset_string="";
		        $sql="alter table $table add ".$newfield["field_name"]." ".$field_type_length." ".$charset_string."  not null comment '".$newfield["field_comment"]."'";
		        //执行把新添加的字段添加到$table中
		        $sysfield->execute($sql);
				$this->reverse_not_redirect($table);
			}
		}
		$this->redirect("index",array("table"=>$table));
	 }
	 public function copyfield(){
		checkget();
		$table=$_GET["table"];
		$field=$_GET["field"];
		$sysfield=M("sysfield");
		$field_info=$sysfield->where("col_eng_name='$table' and field_name='$field'")->find();
		unset($field_info['id']);
		
		$field_name=$field_info['field_name'];
		$field_type=$field_info["field_type"];
		$field_length=$field_info["field_length"];
		$field_comment=$field_info["field_comment"];
		if(!empty($field_length)) 
		   $field_type_length="$field_type($field_length)";
        else
		   $field_type_length="$field_type";
		
		if($field_type=='varchar'||$field_type=='text') 
		   $charset_string="character set utf8 collate utf8_general_ci";
		else
		   $charset_string="";
		//获取复制的字段名称
		$del_suffix_field=substr($field_name,0,-1)."_";
		$old_field_name=$sysfield->where("field_name like '$del_suffix_field'")->order("field_name desc")->getField("field_name");
		if(!empty($old_field_name)){
		  $suffix=substr($old_field_name,-1);
		  if(is_numeric($suffix)){
			  $field_name=substr($field_name,0,-1).($suffix+1);
		  }else{
			  $field_name=substr($field_name,0,-1).(ord($suffix)+1);
		  }
		}
		$field_info['field_name']=$field_name;
		$field_info['sequence']=($sysfield->max("sequence"))+1;
		$sql="alter table $table add $field_name $field_type_length $charset_string  not null comment '$field_comment'";
	    $sysfield=D("sysfield");
		//执行把新添加的字段添加到$table中
		$sysfield->execute($sql);
		$sysfield->add($field_info);
		$this->redirect("index",array("table"=>$table));
	 }
	 public function add_remain_field(){
		checkform();
		set_time_limit(0);
		$table=$_GET["table"];
		$remain_field=$_POST["remain_field"];
		$sysfield=M("sysfield");

		$remain_field_arr=explode(",",$remain_field);
		foreach($remain_field_arr as $k=>$v){
			$field_info=$sysfield->where("field_name='$v' and col_eng_name='$table'")->order("sequence desc")->find();
			if(empty($field_info)){
				continue;
			}
			$newfield=array();
			$newfield["col_eng_name"]=$table;
			$newfield["field_name"]=$field_info["field_name"]."_org_filename";
			$newfield["field_comment"]=$field_info["field_comment"]."_保留原始文件名称";
			$newfield["field_type"]="varchar";
			$newfield["field_length"]=200;
			$newfield["field_output_type"]="无输出框";
			$newfield["field_isuse"]=1;
			$newfield["field_isdetail"]=0;
			$newfield["field_islist"]=0;
			$newfield["sequence"]=$sysfield->where("col_eng_name='$table'")->max("sequence")+1;
			$sysfield->add($newfield);
			
			if(!empty($newfield["field_length"])) 
				$field_type_length=$newfield["field_type"]."(".$newfield["field_length"].")";
			else
				$field_type_length=$newfield["field_type"];
	
			if($newfield["field_type"]=='varchar'||$newfield["field_type"]=='text') 
				$charset_string="character set utf8 collate utf8_general_ci";
			else
				$charset_string="";
			$sql="alter table $table add ".$newfield["field_name"]." ".$field_type_length." ".$charset_string."  not null comment '".$newfield["field_comment"]."'";
			//执行把新添加的字段添加到$table中
			$sysfield->execute($sql);
		}
		$this->redirect("index",array("table"=>$table));
	 }
}
?>